package org.example.log4j;

import java.util.Base64;
import java.util.Random;

public class TBase64 {

    public static void main(String[] args) {

        // String s1 = "sdfsdflsdfsldflsdfsldfsdl";
        //
        // String s2 = "sdfsdssssssssssssssssdkkksdfsdssssssssssssssssdkkk";
        //
        // String s3 = "水电费水电费水电费水电费你好的看见我部分水电费水电费水电费水电费你好的看见我部分";
        //
        // String s1_1 = Base64.getEncoder().encodeToString(s1.getBytes(Charset.forName("UTF-8")));
        // String s2_1 = Base64.getEncoder().encodeToString(s2.getBytes(Charset.forName("UTF-8")));
        //
        // String s3_1 = Base64.getEncoder().encodeToString(s3.getBytes(Charset.forName("UTF-8")));
        // String s4_1 = Base64.getEncoder().encodeToString(s4.getBytes(Charset.forName("UTF-8")));
        //
        // System.out.println("base64编码前 字符长度：s1.length="+s1.length()+" s2.length="+s2.length()+"
        // s3.length="+s3.length());
        // System.out.println("base64编码前 字节长度：s1.length="+s1.getBytes(Charset.forName("UTF-8")).length+"
        // s2.length="+s2.getBytes(Charset.forName("UTF-8")).length+"
        // s3.length="+s3.getBytes(Charset.forName("UTF-8")).length);
        // System.out.println("base64编码后 字节长度：s1.length="+s1_1.length()+" s2.length="+s2_1.length()+"
        // s3.length="+s3_1.length());

        int[] tarr = new int[] {100, 1000, 10000, 100000};

        Base64.Encoder encoder = Base64.getEncoder();
        for (int k : tarr) {
            byte[] bytes = getBytes(k);
            byte[] abytes = encoder.encode(bytes);
            System.out.println(new String(abytes));
            System.out.println("base64编码前 字符长度：length=" + k + " 编码后 length=" + abytes.length);
        }

    }

    private static Random random = new Random();

    /**
     * 生产随机字节数组
     * 
     * @param k
     * @return
     */
    public static byte[] getBytes(int k) {
        byte[] arr = new byte[k];
        for (int i = 0; i < k; ++i) {
            arr[i] = (byte)(random.nextInt(127));
        }
        return arr;
    }

}
